<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="名称" prop="groupName">
        <el-input v-model="drawerProps.row!.groupName" placeholder="请填写名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="编码" prop="groupCode">
        <el-input v-model="drawerProps.row!.groupCode" placeholder="请填写编码" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { genderType } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import { User, RoleManager, dataDictionary } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import { existByRoleCode, existByRoleName, save, editRole } from "@/api/modules/accessController";
import { saveGroup, mergeGroup } from "@/api/modules/dataDictionary";
const rules = reactive({
  groupName: [{ required: true, message: "请输入名称" }],
  groupCode: [{ required: true, message: "请输入编号" }]
});

let roleStateFlag = ref("");

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<dataDictionary.ResDataDictionaryList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      let param = {
        groupName: "",
        groupCode: "",
        groupNo: ""
      };
      param.groupName = drawerProps.value.row!.groupName == undefined ? "" : drawerProps.value.row!.groupName;
      param.groupCode = drawerProps.value.row!.groupCode == undefined ? "" : drawerProps.value.row!.groupCode;
      param.groupNo = drawerProps.value.row!.groupNo == undefined ? "" : drawerProps.value.row!.groupNo;
      if (`${drawerProps.value.title}` == "新增") {
        await saveGroup(param, "ddgDto").then(res => {
          if (res == null) {
            ElMessage.success({ message: `${drawerProps.value.title}组成功！` });
          } else {
            ElMessage.error({ message: `名称或编号重复` });
          }
        });
        drawerProps.value.getTableList!();
        drawerVisible.value = false;
      } else {
        await mergeGroup(param, "ddgDto").then(res => {
          ElMessage.success({ message: `${drawerProps.value.title}组成功！` });
        });
        drawerProps.value.getTableList!();
        drawerVisible.value = false;
      }
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
